package com.bestcem.xm.common.core.domain.web;

import com.bestcem.xm.common.core.constant.Constants;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.io.Serializable;

/**
 * 功能描述： 前后端数据交互统一的类
 * 最后更新时间: 2021年6月4日15:05:55
 */
@NoArgsConstructor
@Getter
@Setter
public class ResultJson<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    @JsonProperty("status_code")
    private Integer status = Constants.ERROR_CODE;
    /**
     * 错误描述
     */
    @JsonProperty("err_msg")
    private String error = "";
    @JsonProperty("err_code")
    private String errorCode = "";

    /**
     * 返回对象
     */
    private T data = null;

    public void retSuccess() {
        this.status = Constants.SUCCESS_CODE;
    }

    public void retError() {
        this.status = Constants.ERROR_CODE;
    }

    public static <T> ResultJson<T> success() {
        ResultJson<T> resultJson = new ResultJson<>();
        resultJson.setStatus(Constants.SUCCESS_CODE);
        return resultJson;
    }

    public static <T> ResultJson<T> success(T value) {
        ResultJson<T> resultJson = new ResultJson<>();
        resultJson.setStatus(Constants.SUCCESS_CODE);
        resultJson.setData(value);
        return resultJson;
    }

    public static <T> ResultJson<T> fail(String errorMsg) {
        ResultJson<T> resultJson = new ResultJson<>();
        resultJson.setStatus(Constants.ERROR_CODE);
        resultJson.setError(errorMsg);
        return resultJson;
    }

    public static <T> ResultJson<T> fail(String errorCode, String errorMsg) {
        ResultJson<T> resultJson = new ResultJson<>();
        resultJson.setStatus(Constants.ERROR_CODE);
        resultJson.setErrorCode(errorCode);
        resultJson.setError(errorMsg);
        return resultJson;
    }

    public static <T> ResultJson<T> fail(String errorCode, String errorMsg, T value) {
        ResultJson<T> resultJson = new ResultJson<>();
        resultJson.setStatus(Constants.ERROR_CODE);
        resultJson.setErrorCode(errorCode);
        resultJson.setError(errorMsg);
        resultJson.setData(value);
        return resultJson;
    }

    public static <T> ResultJson<T> fail(BindingResult paramResult) {
        StringBuilder errorMsgBuilder = new StringBuilder();
        if (paramResult.hasErrors()) {
            paramResult.getAllErrors().forEach((error) -> {
                FieldError fieldError = (FieldError) error;
                // 属性
                String field = fieldError.getField();
                // 错误信息
                String message = fieldError.getDefaultMessage();
                String msg = String.format(field + ":%s", message);
                if (errorMsgBuilder.length() > 0) {
                    errorMsgBuilder.append("|");
                }
                errorMsgBuilder.append(msg);
            });
        }
        return ResultJson.fail(errorMsgBuilder.toString());
    }

    /**
     * 判断是否为错误请求
     */
    public boolean hasError() {
        return Constants.ERROR_CODE.equals(this.status);
    }

    public static <T> ResultJson<T> handelServiceResult(ServiceResult<T> serviceResult) {
        return serviceResult.isSuccess() ? success(serviceResult.getData()) : fail(serviceResult.getMsg());
    }
}